import getDynamicInModelDefinition from './action/DynamicIn';
import getDynamicInOutModelDefinition from './action/DynamicInOut';
import getDynamicOutModelDefinition from './action/DynamicOut';
import { BehaviorTree } from './core/Adapter';
import GlobalVariableManager, { Variable } from './core/GlobalVariableManager';

/**
 * 节点类型
 */
export type NodeType = 'in' | 'out' | 'inout';

/**
 * 注册节点
 * @param data - 节点数据
 */
export const registerNode = (data: any[]) => {
  if (!window.behaviorDesigner) {
    return;
  }
  data.forEach((o) => {
    const { type, text, icon } = o;
    const nodeType = o.nodeType as NodeType;
    const nodeDefinition =
      nodeType === 'in'
        ? getDynamicInModelDefinition(type, text, icon)
        : nodeType === 'out'
        ? getDynamicOutModelDefinition(type, text, icon)
        : getDynamicInOutModelDefinition(type, text, icon);
    window.behaviorDesigner.register(nodeDefinition);
  });
};

/**
 * 渲染图形
 * @param data - 行为树数据
 */
export const render = (data: BehaviorTree) => {
  if (!window.behaviorDesigner) {
    return;
  }
  window.behaviorDesigner.render(data);
};

/**
 * 设置节点属性
 * @param nodeId - 节点 id
 * @param properties - 属性
 */
export const setNodeProperties = (nodeId: string, properties: Record<string, any>) => {
  const node = window.behaviorDesigner.getNodeModelById(nodeId);
  node?.setProperties(properties);
};

/**
 * 获取节点属性
 * @param nodeId - 节点 id
 */
export const getNodeProperties = (nodeId: string) => {
  const node = window.behaviorDesigner.getNodeModelById(nodeId);
  return node?.getProperties();
};

/**
 * 设置全局变量
 * @param properties - 属性
 */
export const setGlobalVariables = (variables: Record<string, Variable>) => {
  GlobalVariableManager.setVariables(variables);
};

/**
 * 获取全局变量
 */
export const getGlobalVariables = () => {
  return GlobalVariableManager.variables;
};

/**
 * 获取图形数据
 * @returns
 */
export const getGraphData = () => {
  if (!window.behaviorDesigner) {
    return;
  }
  return window.behaviorDesigner.getGraphData();
};

/**
 * 绑定拖拽面板
 * @param dndNodeParentClassName
 * @param dndNodeClassName
 * @returns
 */
export const bindDndPanel = (dndNodeParentClassName: string) => {
  if (!window.behaviorDesigner) {
    return;
  }
  const dndNodeParent = document.querySelector(dndNodeParentClassName);
  if (!dndNodeParent) {
    return;
  }
  dndNodeParent.addEventListener('mousedown', onDndNodeMouseDown);
};

/**
 * 解绑拖拽面板
 * @param dndNodeParentClassName
 * @param dndNodeClassName
 * @returns
 */
export const unbindDndPanel = (dndNodeParentClassName: string) => {
  if (!window.behaviorDesigner) {
    return;
  }
  const dndNodeParent = document.querySelector(dndNodeParentClassName);
  if (!dndNodeParent) {
    return;
  }
  dndNodeParent.removeEventListener('mousedown', onDndNodeMouseDown);
};

const onDndNodeMouseDown = (e: Event) => {
  const type = (e.target as HTMLElement).getAttribute('data-bt-task-type')!;
  if (!type) {
    return;
  }
  window.behaviorDesigner.dnd.startDrag({
    type,
  });
};

/**
 * 将整个流程图缩小到画布能全部显示
 * @param paddingX
 * @param paddingY
 * @returns
 */
export const fieView = (paddingX = 360, paddingY = 100) => {
  if (!window.behaviorDesigner) {
    return;
  }
  window.behaviorDesigner.fitView(paddingX, paddingY);
};
